package com.lb.jwitter.action;


import net.sf.json.JSONArray; 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; 
import org.springframework.test.context.ContextConfiguration;
import com.lb.jwitter.util.ApplicationContextProvider; 
import net.sf.json.JSONObject; 
import net.sf.json.JSONSerializer; 
import java.util.Map; 
import com.opensymphony.xwork2.ActionContext; 
import java.text.DateFormat; 
import java.text.SimpleDateFormat; 
import java.util.ArrayList; 
import java.util.Collection; 
import java.util.Collections;
import java.util.Date; 
import java.util.GregorianCalendar; 
import java.util.List;

import javax.xml.datatype.DatatypeFactory; 
import javax.xml.datatype.XMLGregorianCalendar; 
import org.springframework.context.support.ClassPathXmlApplicationContext; 
import com.lb.jwitter.service.ConditionReport; 
import com.lb.jwitter.util.Message; 
import com.opensymphony.xwork2.Action; 
import com.lb.jwitter.service.TblQuyenChiTietService;import com.lb.jwitter.service.TblTaiKhoan;import com.lb.jwitter.service.TblQLDTTXAMonHoc;
import com.lb.jwitter.service.TblQLDTTXAMonHocService;
import com.lb.jwitter.service.TblQLDTTXADMBoMon;
import com.lb.jwitter.service.TblQLDTTXADMBoMonService;

import com.lb.jwitter.service.TblQLDTTXAMonHocTienQuyet;

import com.lb.jwitter.service.TblQLDTTXAMonHocTienQuyetService;

import com.lb.jwitter.service.TblQLDTTXAMonHocBatBuoc;

import com.lb.jwitter.service.TblQLDTTXAMonHocBatBuocService;

import com.lb.jwitter.service.TblQLDTTXAMonHocSongHanh;

import com.lb.jwitter.service.TblQLDTTXAMonHocSongHanhService;

 /** 
 * @author QUOCVIET  
 * 
 */ 


@ContextConfiguration(locations="classpath:beans.xml") 
public class TblQLDTTXAMonHocInsertAction{
    private int pageSize; 
    public int getPageSize() {
       return pageSize;
 }

    public void setPageSize(int pageSize) { this.pageSize = pageSize; }

    private int page; 
    public int getPage() { return page; }

    public void setPage(int page) { this.page = page; }

    private int pages; 
    public int getPages() { return pages; }

    public void setPages(int pages) { this.pages = pages; }

    private String dataString; 
    public String getDataString() { return dataString; }

    private Message msg; 
    public Message getMsg() { return msg; }

    private Long id; 
    public void setId(Long id) {
       this.id = id;
 }

    private String ten; 
    public void setTen(String ten) {
       this.ten = ten;
 }

    public String getTen() {
       return ten;
 }

    private int soTinChi; 
    public void setSoTinChi(int soTinChi) {
       this.soTinChi = soTinChi;
 }

    public int getSoTinChi() {
       return soTinChi;
 }

    private int soTietTrongTuan;
    private int tuChon; 
    public void setTuChon(int tuChon) {
       this.tuChon = tuChon;
 }

    public int getTuChon() {
       return tuChon;
 }

    private String ghiChu; 
    public void setGhiChu(String ghiChu) {
       this.ghiChu = ghiChu;
 }

    public String getGhiChu() {
       return ghiChu;
 }

    private String maMonHoc; 
    public void setMaMonHoc(String maMonHoc) {
       this.maMonHoc = maMonHoc;
 }

    public String getMaMonHoc() {
       return maMonHoc;
 }

    private int boMonId; 
    public void setBoMonId(int boMonId) {
       this.boMonId = boMonId;
 }

    public int getBoMonId() {
       return boMonId;
 }

    private Collection<TblQLDTTXADMBoMon> tblQLDTTXADMBoMons; 
    public Collection<TblQLDTTXADMBoMon>  getTblQLDTTXADMBoMons(){ 
              return tblQLDTTXADMBoMons; 
     } 

    private TblQLDTTXADMBoMonService tblQLDTTXADMBoMonService; 

    private int soTietHoc; 
    public void setSoTietHoc(int soTietHoc) {
       this.soTietHoc = soTietHoc;
 }

    public int getSoTietHoc() {
       return soTietHoc;
 }
    private int phanTramDanhGiaGK;
    public void setPhanTramDanhGiaGK(int phanTramDanhGiaGK){
    	this.phanTramDanhGiaGK = phanTramDanhGiaGK;
    }
    public int getPhanTramDanhGiaGK(){
    	return phanTramDanhGiaGK;
    }
    
    private int phanTramDanhGiaCK;
    public void setPhanTramDanhGiaCK(int phanTramDanhGiaCK){
    	this.phanTramDanhGiaCK = phanTramDanhGiaCK;
    }
    public int getPhanTramDanhGiaCK(){
    	return phanTramDanhGiaCK;
    }
    
    private int phanTramThucHanh;
    public void setPhanTramThucHanh(int phanTramThucHanh){
    	this.phanTramThucHanh = phanTramThucHanh;
    }
    public int getPhanTramThucHanh(){
    	return phanTramThucHanh;
    }

    private TblQLDTTXAMonHoc tblQLDTTXAMonHoc; 
    public TblQLDTTXAMonHoc  getTblQLDTTXAMonHoc() {
        return tblQLDTTXAMonHoc;
 }

    private Collection<TblQLDTTXAMonHoc> tblQLDTTXAMonHocs; 
    public Collection<TblQLDTTXAMonHoc>  getTblQLDTTXAMonHocs() {
         return tblQLDTTXAMonHocs; 
      } 

    private TblQLDTTXAMonHocService tblQLDTTXAMonHocService; 

    private String tblQLDTTXAMonHocTienQuyetStr; 
    public void setTblQLDTTXAMonHocTienQuyetStr(String tblQLDTTXAMonHocTienQuyetStr) 
{
       this.tblQLDTTXAMonHocTienQuyetStr = tblQLDTTXAMonHocTienQuyetStr;
 }

    public String getTblQLDTTXAMonHocTienQuyetStr() 
 {
       return tblQLDTTXAMonHocTienQuyetStr;
 }

    private TblQLDTTXAMonHocTienQuyetService tblQLDTTXAMonHocTienQuyetService; 

    private String tblQLDTTXAMonHocBatBuocStr; 
    public void setTblQLDTTXAMonHocBatBuocStr(String tblQLDTTXAMonHocBatBuocStr) 
{
       this.tblQLDTTXAMonHocBatBuocStr = tblQLDTTXAMonHocBatBuocStr;
 }

    public String getTblQLDTTXAMonHocBatBuocStr() 
 {
       return tblQLDTTXAMonHocBatBuocStr;
 }

    private TblQLDTTXAMonHocBatBuocService tblQLDTTXAMonHocBatBuocService; 

    private String tblQLDTTXAMonHocSongHanhStr; 
    public void setTblQLDTTXAMonHocSongHanhStr(String tblQLDTTXAMonHocSongHanhStr) 
{
       this.tblQLDTTXAMonHocSongHanhStr = tblQLDTTXAMonHocSongHanhStr;
 }

    public String getTblQLDTTXAMonHocSongHanhStr() 
 {
       return tblQLDTTXAMonHocSongHanhStr;
 }

    private TblQLDTTXAMonHocSongHanhService tblQLDTTXAMonHocSongHanhService; 

	private int preInsert(Message msg, TblQLDTTXAMonHoc obj ){ 
        msg.setPreMessage_b(true); 
        msg.setPreMessage_s("success"); 
        return 1; 
    } 
	private int postInsert(Message msg, TblQLDTTXAMonHoc obj ){ 
        msg.setPostMessage_b(true); 
        msg.setPostMessage_s("success"); 
        return 1; 
    } 
  private TblTaiKhoan taiKhoanDangNhap; 
 public TblTaiKhoan getTaiKhoanDangNhap() { 
     return taiKhoanDangNhap; 
 } 
 public void setTaiKhoanDangNhap(TblTaiKhoan taiKhoanDangNhap) { 
     this.taiKhoanDangNhap = taiKhoanDangNhap; 
 } 
 private TblQuyenChiTietService tblQuyenChiTietService;
	 @Autowired // Injected by Spring 
     ApplicationContextProvider contextProvider; 
 public String execute() throws Exception 
{ 
 try{ 	
     Map session = ActionContext.getContext().getSession();
     taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
     if( taiKhoanDangNhap == null) return "noprivilege";
    ApplicationContext context = ApplicationContextProvider.getApplicationContext(); 
    tblQuyenChiTietService = (TblQuyenChiTietService) context.getBean("tbl_Quyen_ChiTietClient"); 
    if(!tblQuyenChiTietService.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(), "tbl_QLDTTXA_MonHoc", "them")){ 
        return "noprivilege"; 
    }        
    
    	tblQLDTTXAMonHocService = (TblQLDTTXAMonHocService) context.getBean("tbl_QLDTTXA_MonHocClient"); 
        tblQLDTTXADMBoMonService = (TblQLDTTXADMBoMonService) context.getBean("tbl_QLDTTXA_DM_BoMonClient"); 

        tblQLDTTXAMonHocTienQuyetService = (TblQLDTTXAMonHocTienQuyetService) context.getBean("tbl_QLDTTXA_MonHocTienQuyetClient"); 

        tblQLDTTXAMonHocBatBuocService = (TblQLDTTXAMonHocBatBuocService) context.getBean("tbl_QLDTTXA_MonHocBatBuocClient"); 

        tblQLDTTXAMonHocSongHanhService = (TblQLDTTXAMonHocSongHanhService) context.getBean("tbl_QLDTTXA_MonHocSongHanhClient"); 

        msg = new Message(); 
        tblQLDTTXAMonHoc = new TblQLDTTXAMonHoc(); 
        tblQLDTTXAMonHoc.setTen(ten); 
        tblQLDTTXAMonHoc.setSoTinChi(soTinChi);
        tblQLDTTXAMonHoc.setSoTietTrongTuan(soTietTrongTuan);
        if(tuChon == 1){ 
           tblQLDTTXAMonHoc.setTuChon(true); 
       } else { 
           tblQLDTTXAMonHoc.setTuChon(false); 
       } 
        tblQLDTTXAMonHoc.setGhiChu(ghiChu); 
        tblQLDTTXAMonHoc.setMaMonHoc(maMonHoc); 
        tblQLDTTXAMonHoc.setBoMonId(tblQLDTTXADMBoMonService.gettblQLDTTXADMBoMon(new Long(boMonId))); 
        tblQLDTTXAMonHoc.setSoTietHoc(soTietHoc); 
        tblQLDTTXAMonHoc.setPhanTramDanhGiaCK(new Double(phanTramDanhGiaCK));
        tblQLDTTXAMonHoc.setPhanTramDanhGiaGK(new Double(phanTramDanhGiaGK));
        tblQLDTTXAMonHoc.setPhanTramThucHanh(new Double(phanTramThucHanh));
        preInsert(msg,tblQLDTTXAMonHoc); 
       if (msg.isPreMessage_b()) 
       { 
           id = tblQLDTTXAMonHocService.createtblQLDTTXAMonHoc(tblQLDTTXAMonHoc); 
           postInsert(msg, tblQLDTTXAMonHoc); 
           if (msg.isPostMessage_b() == false) 
           { 
               return Action.ERROR; 
           } 
       }else{ 
           return Action.ERROR; 
       } 
       tblQLDTTXAMonHoc.setId(id); 
		

       JSONArray jsonTblQLDTTXAMonHocTienQuyet = (JSONArray)JSONSerializer.toJSON(tblQLDTTXAMonHocTienQuyetStr); 
   	
   	for (int i = 0; i < jsonTblQLDTTXAMonHocTienQuyet.size(); i++) 
   	 { 
   	      JSONObject jsObj = jsonTblQLDTTXAMonHocTienQuyet.getJSONObject(i); 
   	        TblQLDTTXAMonHocTienQuyet obj0 = new TblQLDTTXAMonHocTienQuyet(); 
   	       obj0.setMonHocId(tblQLDTTXAMonHoc); 

   	       TblQLDTTXAMonHoc tblQLDTTXAMonHocTienQuyet = new TblQLDTTXAMonHoc();
   	       tblQLDTTXAMonHocTienQuyet.setId(new Long (jsObj.getInt("monHocTienQuyetId")) );
   	       obj0.setMonHocTienQuyetId(tblQLDTTXAMonHocTienQuyet);
   	       
   	       tblQLDTTXAMonHocTienQuyetService.createtblQLDTTXAMonHocTienQuyet(obj0); 
   	   } 
   	
       
       
       JSONArray jsonTblQLDTTXAMonHocBatBuoc = (JSONArray)JSONSerializer.toJSON(tblQLDTTXAMonHocBatBuocStr); 
   	for (int i = 0; i < jsonTblQLDTTXAMonHocBatBuoc.size(); i++) 
   	 { 
   	      JSONObject jsObj = jsonTblQLDTTXAMonHocBatBuoc.getJSONObject(i); 
   	        TblQLDTTXAMonHocBatBuoc obj1 = new TblQLDTTXAMonHocBatBuoc(); 
   	       obj1.setMonHocId(tblQLDTTXAMonHoc); 
   	       

   	       TblQLDTTXAMonHoc tblQLDTTXAMonHocBatBuoc = new TblQLDTTXAMonHoc();
   	       tblQLDTTXAMonHocBatBuoc.setId(new Long (jsObj.getInt("monHocBatBuocId")) );
   	       obj1.setMonHocBatBuocId(tblQLDTTXAMonHocBatBuoc);
   	       
   	       tblQLDTTXAMonHocBatBuocService.createtblQLDTTXAMonHocBatBuoc(obj1); 
   	   } 
       
       
       JSONArray jsonTblQLDTTXAMonHocSongHanh = (JSONArray)JSONSerializer.toJSON(tblQLDTTXAMonHocSongHanhStr); 
       for (int i = 0; i < jsonTblQLDTTXAMonHocSongHanh.size(); i++) 
        { 
            JSONObject jsObj = jsonTblQLDTTXAMonHocSongHanh.getJSONObject(i); 
            TblQLDTTXAMonHocSongHanh obj2 = new TblQLDTTXAMonHocSongHanh(); 
          obj2.setMonHocId(tblQLDTTXAMonHoc); 
          
          TblQLDTTXAMonHoc tblQLDTTXAMonHocSongHanh = new TblQLDTTXAMonHoc();
          tblQLDTTXAMonHocSongHanh.setId(new Long (jsObj.getInt("monHocSongHanhId")) );
          obj2.setMonHocSongHanhId(tblQLDTTXAMonHocSongHanh);

          tblQLDTTXAMonHocSongHanhService.createtblQLDTTXAMonHocSongHanh(obj2); 
      } 
		        return Action.SUCCESS; 
      } 
      catch(Exception e){ 
         System.out.println("Loi Insert tbl_QLDTTXA_MonHoc Action"); 
         e.printStackTrace(); 
         return Action.ERROR; 
 } 
 }

public int getSoTietTrongTuan() {
	return soTietTrongTuan;
}

public void setSoTietTrongTuan(int soTietTrongTuan) {
	this.soTietTrongTuan = soTietTrongTuan;
} 
 } 
 
